package com.nowcoder.community.util;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;

public class CookieUtil {

    public static String getValue(HttpServletRequest request, String key) {
        if(request == null) {
            throw new IllegalArgumentException("参数为空!");
        }
        if(StringUtils.isBlank(key)) {
            throw new IllegalArgumentException("参数为空!");
        }
        Cookie[] cookies = request.getCookies();
        if(cookies != null) {
            for(Cookie c : cookies) {
                if(c.getName().equals(key)) {
                    return c.getValue();
                }
            }
        }
        return null;
    }

}
